<?php
/**
 * Author: lf
 * Blog: https://blog.feehi.com
 * Email: job@feehi.com
 * Created at: 2017-08-30 19:04
 */

namespace api\models;

use common\helpers\Util;
use yii\helpers\Url;
use Yii;

class User extends \common\models\User
{
    public function fields()
    {
        $fields = parent::fields();
        unset(
            $fields['auth_key'],
            $fields['password_hash'],
            $fields['password_reset_token'],
            $fields['access_token'],
            $fields['status'],
            $fields['expire_at'],
            $fields['updated_at']
        );
        $fields['avatar']     = function ($model) {
            return $model->avatarUrl;
        };
        $fields['isfellowed'] = function ($model) {
            $user = User::findIdentityByAccessToken(Yii::$app->request->getHeaders()->get('access-token'));
            if ($model->id != $user->id) {
                return Fans::findOne(['cid' => $user->id, 'ccid' => $model->id]) != null ? true : false;
            } else {
                return true;
            }
        };
        $fields['fellows']    = function ($model) {
            return Fans::find()->where(['cid' => $model->id])->count();
        };
        $fields['fans']       = function ($model) {
            return Fans::find()->where(['ccid' => $model->id])->count();
        };

        return $fields;
    }

    public static function getUserByToken(){
        return User::findIdentityByAccessToken(\Yii::$app->request->getHeaders()->get('access-token'));
    }
}
